#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/6/6 20:05
# @Author : 闫峰
# @File    : activite.py
import json
import requests
from urllib import parse

from retrying import retry

from third_services.allegro.RebatyesAndPromotions.baseutils import get_token
from utils.mysqlUtils.mongodb_utils import MongoDBLink

user = parse.quote_plus("admin")
passwd = parse.quote_plus("Zhcx@2023#MonDb")
modb1 = MongoDBLink(
    uri=f'mongodb://{user}:{passwd}@116.205.233.77:8807/?authSource=admin',
    dbname='walmart_api_table', coll_name='web_logs_allegro')


class OfferDetails:

    def __init__(self):
        self.base_url = 'https://api.allegro.pl'
        self.count = 0

    @retry(stop_max_attempt_number=30, wait_fixed=2000)
    def get_offer_details(self, orderSourceid, offerId, campaignId):
        # https://api.{environment}/sale/badges/offers/{offerId}/campaigns/{campaignId}
        requesu_dir = f"/sale/badges/offers/{offerId}/campaigns/{campaignId}"
        headers = {
            "Accept": "application/vnd.allegro.public.v1+json",
            "Content-Type": "application/vnd.allegro.public.v1+json",
            "Authorization": get_token(orderSourceid),
            "Accept-Language": "en-US"
        }
        data = {
           "process":{"status": "FINISHED"}
        }
        res = requests.patch(url=self.base_url + requesu_dir, headers=headers, json=data)
        if res.status_code == 202:
            print(f"{offerId}:{res.json()}")
        else:
            print(f"status_code!=202 重试{self.count}次")
            self.count += 1
            raise Exception(f"status_code!=202")



    @retry(stop_max_attempt_number=30, wait_fixed=2000)
    def sale_badges(self, orderSourceid, offerId):
        requesu_dir = f"/sale/badges"
        headers = {
            "Accept": "application/vnd.allegro.public.v1+json",

            "Authorization": get_token(orderSourceid),
            "Accept-Language": "en-US"
        }
        data = {
            "offer.id": offerId
        }
        res = requests.get(url=self.base_url + requesu_dir, headers=headers, params=data)
        if res.status_code == 200:
            rest = res.json()
            for activity in rest.get('badges'):
                if activity['campaign'].get('name') == 'Allegro Days API 03.06-09.06.2024':
                    print(f"{offerId}:{activity['campaign']}")
        else:
            print(f"status_code!=200 重试{self.count}次")
            self.count += 1
            raise Exception(f"status_code!=200")


if __name__ == '__main__':
    # OfferDetails().sale_badges(19984,14727276133)
    # OfferDetails().get_offer_details(20004, 13667767480, "ALLDEALS_202406_CZ")
    # OfferDetails().get_offer_details(10783,15401752441,"ALLDEALS_202406_CZ")
    """
14652417560
14391887644
15025318517




    """
    offer_id_list = [15115736770,15374472006,15304915975,15304558807,15304283534]
    offer_id_list = [14327955874, 14755307683, 14684858179, 14727276133, 14644561318, 14728088839, 14950339723, 14755229933, 14802228622, 14739839568, 14875905552, 14328479897, 14833601660]
    for offer_id in offer_id_list:
        # OfferDetails().sale_badges(19984, offer_id)
        OfferDetails().get_offer_details(19984, offer_id, "SALE_CATEGORY_ALLDEALS_API_202406_PL")